package com.bolun998300.personal_blog_springboot.controller.background;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.bolun998300.personal_blog_springboot.Utils.DateUtil;
import com.bolun998300.personal_blog_springboot.Utils.Result;
import com.bolun998300.personal_blog_springboot.Utils.WebFileUtil;
import com.bolun998300.personal_blog_springboot.pojo.Blogger;
import com.bolun998300.personal_blog_springboot.service.BloggerService;

@RestController
public class BloggerController {
	
	@Autowired
	private BloggerService bloggerService;
	
	@GetMapping("/bloggers/{id}")
	public Blogger get(@PathVariable int id) {
		return bloggerService.get(id);
	}
	
	@PutMapping("/bloggers")
	public Object update(@RequestBody Blogger bean) {
		Blogger blogger = bloggerService.get(1);
		blogger.setUserName(bean.getUserName());
		blogger.setNickName(bean.getNickName());
		blogger.setSign(bean.getSign());
		blogger.setProfile(bean.getProfile());
		blogger.setImageName(bean.getImageName());
		bloggerService.update(blogger);
		return Result.success();
	}

	@GetMapping("/bloggers/logined")
	public Object get(){
		Subject subject = SecurityUtils.getSubject();
		Blogger blogger = (Blogger)subject.getSession().getAttribute("user");

		return blogger;
	}
 

	
	@PostMapping("/bloggers/login")
	public Result login(String userName,String password, String code,HttpSession session) {
		Subject subject = SecurityUtils.getSubject();
		String rightCode = (String) session.getAttribute("rightCode");
		if (!rightCode.equals(code)) {
			return Result.fail("验证码错误！");
		}else {
			UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
			try {
				subject.login(token);
				Session sessionSub = subject.getSession();
				Blogger user = bloggerService.getByName(userName);
				sessionSub.setAttribute("user", user);
				return Result.success();
			}catch (Exception e) {
				String message ="账号或密码错误";
	            return Result.fail(message);
			}
		}
	}
	
	
	@PutMapping("/bloggers/passwordUpdate")
	public Object passwordUpdate(@RequestBody Blogger bean) {
		Blogger blogger = bloggerService.get(1);
		blogger.setPassword(bean.getPassword());
		bloggerService.update(blogger);
		return Result.SUCCESS_CODE;
	}
	
	@PostMapping("/bloggers/uploadFaceImg")
	public HashMap<String,Object> uploadFaceImg( MultipartFile faceImg,
            HttpServletRequest request){
		OutputStream os = null;
        InputStream is = null;
        try {
        	String fileName = faceImg.getOriginalFilename();
            String fileType = fileName.split("\\.")[1];
            String filePath=WebFileUtil.getSystemRootPath(request);
            String newFileName = "faceImg"+DateUtil.getCurrentDateStr()+"."+fileType;
        	
            String newFolder="/uploadImage/faceImg/";
            String newFilePath=filePath+newFolder;
            WebFileUtil.createFold(new File(newFilePath));
            os = new FileOutputStream(newFilePath+newFileName);
            is = faceImg.getInputStream();
            IOUtils.copy(is, os);
            String url = request.getScheme() + "://" + request.getServerName()
            + ":" + request.getServerPort() +"/personal_blog"+ newFolder + newFileName;
            HashMap<String,Object> map = new HashMap<String,Object>();
            map.put("code", 0);
            map.put("msg", "");
            	Map<String,Object> map1 = new HashMap<String,Object>();
            	map1.put("src", url);
            map.put("data", map1);
            return map;
        }catch (Exception e) {
		}finally {
            try {
				is.close();
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
		return null;
	}
	
	
	
	
	
	
	

}
